import 'package:flutter/material.dart';

class MyView extends StatelessWidget {
  final bool visible; // 是否显示
  final dynamic color; // 背景色
  final AssetImage? image; // 背景图
  final LinearGradient? gradient; // 渐变背景色
  final List<BoxShadow>? boxShadow; // 边框阴影
  final Alignment? alignment; // 对齐方式
  final double? height;
  final double? width;
  final double? margin; // 外边距
  final double? marginTop;
  final double? marginLeft;
  final double? marginRight;
  final double? marginBottom;
  final double? padding; // 内边距
  final double? paddingTop;
  final double? paddingLeft;
  final double? paddingRight;
  final double? paddingBottom;
  final BorderSide? border; // 边框
  final BorderSide? borderLeft;
  final BorderSide? borderRight;
  final BorderSide? borderTop;
  final BorderSide? borderBottom;
  final double? borderRadius; // 边框弧度
  final double? borderTopLeftRadius;
  final double? borderTopRightRadius;
  final double? borderBottomRightRadius;
  final double? borderBottomLeftRadius;
  final Widget? child; // 子组件
  final List<Positioned>? children; // 相对定位
  final void Function()? onTap; // 单击事件

  const MyView(
      {super.key,
      this.visible = true,
      this.color,
      this.image,
      this.gradient,
      this.boxShadow,
      this.alignment,
      this.width,
      this.height,
      this.margin,
      this.marginTop,
      this.marginLeft,
      this.marginRight,
      this.marginBottom,
      this.padding,
      this.paddingTop,
      this.paddingLeft,
      this.paddingRight,
      this.paddingBottom,
      this.border,
      this.borderLeft,
      this.borderRight,
      this.borderTop,
      this.borderBottom,
      this.borderRadius,
      this.borderTopLeftRadius,
      this.borderTopRightRadius,
      this.borderBottomRightRadius,
      this.borderBottomLeftRadius,
      this.child,
      this.onTap,
      this.children});

  // 边距
  static EdgeInsetsGeometry edgeInsets(
    double? all,
    double? top,
    double? left,
    double? right,
    double? bottom,
  ) {
    return EdgeInsets.only(
      top: all ?? top ?? 0.0,
      left: all ?? left ?? 0.0,
      right: all ?? right ?? 0.0,
      bottom: all ?? bottom ?? 0.0,
    );
  }

  @override
  Widget build(BuildContext context) {
    if (!visible) return SizedBox();
    Widget container = Container(
      alignment: alignment,
      width: width,
      height: height,
      margin: edgeInsets(
        margin,
        marginTop,
        marginLeft,
        marginRight,
        marginBottom,
      ),
      padding: edgeInsets(
        padding,
        paddingTop,
        paddingLeft,
        paddingRight,
        paddingBottom,
      ),
      decoration: BoxDecoration(
        color: color != null ? (color is Color ? color : Color(int.parse('0xFF$color'))) : null, // 背景色
        image: image != null ? DecorationImage(image: image!, fit: BoxFit.cover) : null, // 背景图片
        gradient: gradient, // 渐变背景色
        boxShadow: boxShadow, // 边框阴影
        border: Border(
          top: border ?? borderTop ?? BorderSide.none,
          left: border ?? borderLeft ?? BorderSide.none,
          right: border ?? borderRight ?? BorderSide.none,
          bottom: border ?? borderBottom ?? BorderSide.none,
        ),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(borderRadius ?? borderTopLeftRadius ?? 0.0),
          topRight: Radius.circular(borderRadius ?? borderTopRightRadius ?? 0.0),
          bottomLeft: Radius.circular(borderRadius ?? borderBottomLeftRadius ?? 0.0),
          bottomRight: Radius.circular(borderRadius ?? borderBottomRightRadius ?? 0.0),
        ),
      ),
      child: child,
    );
    // 单击事件
    if (onTap != null) {
      container = GestureDetector(
        onTap: onTap, // 点击事件
        child: container,
      );
    }
    // 相对定位
    if (children != null) {
      container = Stack(
        children: [
          container,
          ...children!,
        ],
      );
    }
    return container;
  }
}
